package org.tity.commons.code;


import org.tity.commons.bean.BeanUtil;
import org.tity.commons.log.Logger;

import com.google.common.base.Function;
import com.google.javascript.jscomp.CommandLineRunner;

/**
 * @author LeeFangJie 代码压缩器
 */
public class JSCompressor {

	private static Function<Integer, Void> empty = new Function<Integer, Void>() {
		@Override
		public  Void apply(Integer input) {
			Logger.info("compress ok");
			return null;
		}
	};
	
	private static class CommandLineRunnerX extends  CommandLineRunner
	{
		protected CommandLineRunnerX(String[] args) {
			super(args);
		}
	}
	
	/**
	 * JS 压缩
	 * 
	 * @param args 压缩参数
	 */
	public static void compress(String[] args) {
		CommandLineRunner runner = new CommandLineRunnerX(args);
		if (runner.shouldRunCompiler()) {
			//替换属性值，使其避免退出
			BeanUtil.setFieldValue(runner, "exitCodeReceiver", empty);
			runner.run();
		}
	}

	/**
	 * JS 压缩
	 * 
	 * @param in  输入文件
	 * @param out 输出文件
	 */
	public static void compress(String in, String out) {
		String[] args = { "--js", in, "--js_output_file", out, "--compilation_level", "SIMPLE" };
		compress(args);
	}

}


